package com.tiepbm.cucphamgiadinh.activity;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.MotionEvent;
import android.view.View;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.widget.Button;
import android.widget.Toast;

import com.tiepbm.cucphamgiadinh.R;
import com.tiepbm.cucphamgiadinh.util.MyPreferenceManager;

public class ReadChap extends Activity implements IScreen {

	private WebView webView;
	private Button btPrevious, btNext;
	private int pos = 1;
	private int maxchapter;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_read);
		initView();
		implementListener();
		fillData();
	}

	@Override
	public void initView() {
		// TODO Auto-generated method stub
		btNext = (Button) findViewById(R.id.btnext);
		btPrevious = (Button) findViewById(R.id.btprevious);

		webView = (WebView) findViewById(R.id.webView1);
		WebSettings webSettings = webView.getSettings();
		// webSettings.setTextSize(WebSettings.TextSize.SMALLEST);
		webSettings.setDefaultFontSize(18);
		
	}

	@Override
	public void implementListener() {
		// TODO Auto-generated method stub
		btNext.setOnClickListener(new View.OnClickListener() {

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				pos++;
//				fillWebView(pos);
				Intent intent = new Intent(ReadChap.this, ReadChap.class);
				Bundle rb = new Bundle();
				rb.putInt("chap", pos);
				intent.putExtras(rb);
				startActivity(intent);
				finish();
			}
		});

		btPrevious.setOnClickListener(new View.OnClickListener() {

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				pos--;
//				fillWebView(pos);
				Intent intent = new Intent(ReadChap.this, ReadChap.class);
				Bundle rb = new Bundle();
				rb.putInt("chap", pos);
				intent.putExtras(rb);
				startActivity(intent);
				finish();
			}
		});
	}

	@Override
	public void fillData() {
		// TODO Auto-generated method stub
		Bundle rb = this.getIntent().getExtras();
		pos = rb.getInt("chap");
		System.out.println("POS: "+pos);
		maxchapter = MyPreferenceManager.getInstance(this).getChapterMax();
		fillWebView(pos);
	}

	private String loadAssetTextAsString(Context context, String name) {
		BufferedReader in = null;
		try {
			StringBuilder buf = new StringBuilder();
			InputStream is = context.getAssets().open(name);
			in = new BufferedReader(new InputStreamReader(is));
			String str;
			boolean isFirst = true;

			while ((str = in.readLine()) != null) {
				if (isFirst)
					isFirst = false;
				else
					buf.append("<br>");
				buf.append(str);
			}
			return buf.toString();
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			if (in != null) {
				try {
					in.close();
				} catch (IOException e) {
				}
			}
		}

		return null;
	}

	private void fillWebView(int pos) {
		if (pos == 1)
			btPrevious.setVisibility(View.GONE);
		else
			btPrevious.setVisibility(View.VISIBLE);

		if (pos == maxchapter) {
			btNext.setVisibility(View.GONE);
		} else
		{
			btNext.setVisibility(View.VISIBLE);
		}
		
		MyPreferenceManager mypreference = MyPreferenceManager
				.getInstance(this);
		mypreference.setChaper(pos);
		String html = loadAssetTextAsString(getApplicationContext(),
				"cpgd/" + pos + ".txt");
		if (html != null) {
			String customHtml = "<html><head><META http-equiv=Content-Type content =\"text/html; charset=utf-8\"></head> <body style=\"text-align:justify\">"
					+ html + "</body></html>";
			webView.loadDataWithBaseURL("", customHtml, "text/html", "UTF-8",
					"");
		} else {
			Toast.makeText(getApplicationContext(), "Nội dung đang được cập nhật.",
					Toast.LENGTH_SHORT).show();
		}
	}
}
